package test.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.authz.ModularRealmAuthorizer;
import org.apache.shiro.authz.permission.WildcardPermissionResolver;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;

public class App {
	
	public static void main(String[] args) {
		DefaultSecurityManager securityManager = new DefaultSecurityManager();
		//设置authenticator
		ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
		authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
		securityManager.setAuthenticator(authenticator);

		//设置authorizer
		ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
		authorizer.setPermissionResolver(new WildcardPermissionResolver());
		securityManager.setAuthorizer(authorizer);

		//设置Realm
//		DruidDataSource ds = new DruidDataSource();
//		ds.setDriverClassName("com.mysql.jdbc.Driver");
//		ds.setUrl("jdbc:mysql://localhost:3306/shiro");
//		ds.setUsername("root");
//		ds.setPassword("");

//		JdbcRealm jdbcRealm = new JdbcRealm();
//		jdbcRealm.setDataSource(ds);
//		jdbcRealm.setPermissionsLookupEnabled(true);
//		securityManager.setRealms(Arrays.asList((Realm) jdbcRealm));
		
		SimpleAccountRealm sar = new SimpleAccountRealm();
		sar.addAccount("admin", "admin");
		securityManager.setRealm(sar);

		//将SecurityManager设置到SecurityUtils 方便全局使用
		SecurityUtils.setSecurityManager(securityManager);

		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("admin", "admin");
		subject.login(token);
		System.out.println(subject.isAuthenticated());

		 
	}

}
